<template>
	<view class="container">
		<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" :options="options"
			:collection="collectionList" :getone="true" >
			<view v-if="error">{{error.message}}</view>
			<view v-else-if="loading">
				<uni-load-more :contentText="loadMore" status="loading"></uni-load-more>
			</view>
			<view v-else-if="data">
				<view>
					<uni-title type="h2" align="center" :title="data.title"></uni-title>
				</view>
				<view class="art-content">
					<rich-text :nodes="delta2html(data.content)"></rich-text>
				</view>
				<view class="date_r">
					<uni-dateformat format="(yyyy/MM/dd)" :date="data.last_modify_date"></uni-dateformat>
				</view>
			</view>
		</unicloud-db>
		<!--    <view class="btns">
      <button type="primary" @click="handleUpdate">修改</button>
      <button type="warn" class="btn-delete" @click="handleDelete">删除</button>
    </view> -->
		<up-gap height="70"></up-gap>
	</view>

</template>

<script lang="ts">
	// 由schema2code生成，包含校验规则和enum静态数据
	//import type Delta from 'quill-delta';
	import { ref } from 'vue';
	import { Delta } from 'quill-delta-converter/dist/types';
	import { DeltaConverter } from 'quill-delta-converter';
	import { enumConverter } from '@/js_sdk/validator/uni-cms-articles.js';
	// 引入转换器，用于将quill-delta格式的content转换成HTML格式，并支持markdown语法高亮显示。

	const db = uniCloud.databaseForJQL()
	//require('@/js_sdk/quill-delta-converter/index.js')

	export default {
		setup(){
			const udb = ref(null) // 初始化unicloud-db组件实例，用于调用loadData方法刷新数据
			let id = '' // 初始化id，用于查询单条数据
			return {udb,id}
		},
		data() {
			return {
				//id: '',
				queryWhere: '',
				collectionList: [
					db.collection('uni-cms-articles')
					.field('category_id,title,excerpt,article_status,last_modify_date,content')
					.getTemp(), 
					db.collection('uni-cms-categories')
					.field('name as text, _id')
					.getTemp()],
				loadMore: {
					contentdown: '',
					contentrefresh: '',
					contentnomore: ''
				},
				options: {
					// 将scheme enum 属性静态数据中的value转成text
					...enumConverter
				}
			}
		},
		onLoad(e) {
			console.log("id",e)
			this.id = e.id
			console.log("id",this.id)
			if (this.id) {
				this.collectionList = [
					db.collection('uni-cms-articles')
					.where('_id=="' + this.id + '"')
					.field('category_id,title,excerpt,article_status,last_modify_date,content')
					.getTemp(), 
					db.collection('uni-cms-categories').field('name as text, _id')
					.getTemp()
					]
			}
		},
		onReady() {
			
		},
		methods: {
			delta2html(deltaData: Delta) {
				console.log("deltaData",deltaData)
				const converter = new DeltaConverter();
				let html=converter.toHtml(deltaData);
				return html
			},
			handleUpdate() {
				// 打开修改页面
				uni.navigateTo({
					url: './edit?id=' + this.id,
					events: {
						// 监听修改页面成功修改数据后, 刷新当前页面数据
						refreshData: () => {
							this.udb.loadData({
								clear: true
							})
						}
					}
				})
			},
			handleDelete() {
				this.udb.remove(this.id, {
					success: () => {
						// 删除数据成功后跳转到list页面
						uni.navigateTo({
							url: './list'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 20px;
		line-height: 2em;
	}

	.art-title {
		color: $u-primary;
	}

	.art-content {
		color: $u-content-color;
		text-indent: 2em;
		text-align: justify;
		text-align-last: left;

		ol {
			padding: .5em 0 .5em 3em;
			text-indent: 0em;
		}
	}

	.date_r {
		float: right;
		color: $u-light-color;
	}

	.btns {
		margin-top: 10px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
	}

	.btns button {
		flex: 1;
	}

	.btn-delete {
		margin-left: 10px;
	}
</style>